<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Request;

use app\api\validate\CheckValidate;
use app\api\model\TelCode;
use app\api\model\MemberTmp;
use app\api\model\Member;
use app\api\model\Banner;
use app\api\model\Course;
use app\api\model\CourseMenu;
use app\api\model\Lecturer;
use app\api\model\CourseSubject;
use app\api\model\CourseCate;
use app\api\model\Feedback;
use app\api\model\Base;


class Tourist extends Controller
{
    /**
    * 试看列表
    */
    public function trySee(){
        $model = new Course;
        $where = [['is_try_see', '=', 1]];
        $list = $model->_list($where);
        die(apiSuccess($list));
    }


    /**
    * 试看详情
    */
    public function vedioDetail(){
        $request = $this->request->post();
        $id = (int)$request['id']; //课程id
        if(empty($id)){
            die(apiError('课程id必须'));
        }
        
        $model = new Course;
        $course = $model->_getInfoByID($id);
        
        if(empty($course)){
            die(apiError('课程不存在'));
        }
        $class_id = $course['class_id'];
        $subject = $model->_getList($class_id);
        $model = new CourseMenu;
        $detail = [];
        $list = [];
        $lecturer = [];
        $detail = $model->_getByCid($id);
        $where = ['a.cid'=>$id];
        $lid = $course['lid'];
        if(!empty($request['lid'])){
            $where['a.lid'] = $request['lid'];
        }else{
            $where['a.lid'] = $detail['lid'];
        }
        
        if(!empty($request['version'])){
            $where['a.is_new'] = $request['version'];
        }else{
            $where['a.is_new'] = $detail['is_new'];
        }
        $list = $model->_list($where);
        $model = new Lecturer;
        
        $lecturer = $model->_getListByWhere([['id', 'in', $lid]]);
        

        
        $data = [
            'info'=>$detail,
            'list'=>$list,
            'lecturer'=>$lecturer,
            'subject'=>$subject,
        ];
        
        die(apiSuccess($data));
    }
    
    /**
    * 课程分类列表
    */
    public function courseCate(){
        $model = new CourseCate;
        $list = $model->_getAll();
        die(apiSuccess($list));
    }
    
    /**
    * 意见反馈
    */
    public function feedback(){
        $request = $this->request->post();
        if(empty($request['tel'])){
            die(apiError('请输入手机号'));
        }
        if(empty($request['content'])){
            die(apiError('请输入内容'));
        }
        
        $model = new Feedback;
        $ret = $model->_create($request['tel'], $request['content']);
        if(!$ret){
            die(apiError($model->getError()));
        }
        die(apiSuccess([], '反馈成功'));
    }
    
    /**
    * 关于我们
    */
    public function aboutUs(){
        $model = new Base;
        $config = $model->getConfig();
        $content = $config['ABOUT_US'];
        die(apiSuccess(['content'=>$content]));
    }
    
    /**
    * 帮助中心
    */
    public function help(){
        $model = new Base;
        $config = $model->getConfig();
        $content = $config['HELP_CENTER'];
        die(apiSuccess(['content'=>$content]));
    }

    /**
     * 帮助中心
     */
    public function service_phone(){
        $model = new Base;
        $config = $model->getConfig();
        $content = $config['SHORT_MSG_OPEN'];
        die(apiSuccess(['content'=>$content]));
    }

    /**
     * 获取最新版本号
     */
    public function get_version(){
        $data = Db::name('version')->where('status',1)->order('id desc')->find();
        die(apiSuccess(['content'=>$data]));
    }
}
